package com.manor.common.dialog

import android.app.Activity
import android.text.TextUtils
import android.view.WindowManager
import android.widget.Toast
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.manor.common.Constants
import com.manor.common.R
import com.manor.common.utils.KLog
import com.netease.nis.captcha.Captcha
import com.netease.nis.captcha.CaptchaConfiguration
import com.netease.nis.captcha.CaptchaListener


/**
 *
 * @date  2020/12/30  11:14
 * @version 1.0
 */
class XVerifyPopup(val mAct: Activity, val listener: (Boolean, String) -> Unit) : CenterPopupView(mAct) {

    private var captcha: Captcha? = null

    companion object {
        fun load(context: Activity, callBack: SimpleCallback = SimpleCallback(), listener: (Boolean, String) -> Unit) {
            KLog.e("log--->", "XVerifyPopup----------")
            XPopup.Builder(context)
                    .isDestroyOnDismiss(true)
                    .setPopupCallback(callBack)
                    .asCustom(XVerifyPopup(context, listener))
                    .show()
        }
    }

    override fun getImplLayoutId(): Int = R.layout.dialog_x_verify


    override fun onCreate() {
        super.onCreate()

        val configuration = CaptchaConfiguration.Builder()
                .captchaId(Constants.SECRET.WyAppId) // 验证码业务id
                // 验证码类型，默认为常规验证码（滑块拼图、图中点选、短信上行），如果要使用智能无感知请设置该类型，否则无需设置
                .mode(CaptchaConfiguration.ModeType.MODE_CAPTCHA)
                .listener(captchaListener) //设置验证码回调监听器
                .build(mAct) // Context，请使用Activity实例的Context

        captcha = Captcha.getInstance().init(configuration)
        captcha?.validate()
    }

    override fun dismiss() {
        super.dismiss()
        captcha?.destroy()
    }

    private val captchaListener = object : CaptchaListener {
        override fun onReady() {
        }

        override fun onValidate(result: String?, validate: String?, msg: String?) {
            KLog.i("captchaListener", "onValidate result = $result , validate = $validate , msg = $msg")
            if (!TextUtils.isEmpty(validate)) {
                //ToastUtils.showShort("验证成功:$validates")

                mAct.runOnUiThread {
                    listener.invoke(true, validate ?: "")
                    dismiss()
                }

            } else {
                Toast.LENGTH_LONG
                //ToastUtils.showShort("验证失败:$msg")
            }
        }

        override fun onError(code: Int, msg: String?) {
            KLog.i("captchaListener", "onError code = $code ,  msg = $msg")
            //ToastUtils.showShort("验证出错，错误码:$code 错误信息:$msg")
            mAct.runOnUiThread {
                listener.invoke(false, "")
                dismiss()
            }
        }

        override fun onClose(closeType: Captcha.CloseType?) {
            KLog.i("captchaListener", "onClose closeType = $closeType ")
            when (closeType) {
                Captcha.CloseType.USER_CLOSE -> {
                    //ToastUtils.showShort("用户关闭验证码")
                    mAct.runOnUiThread {
                        listener.invoke(false, "")
                        dismiss()
                    }
                }
                Captcha.CloseType.VERIFY_SUCCESS_CLOSE -> {
                    //ToastUtils.showShort("校验通过，流程自动关闭")
                }
                else -> {
                    mAct.runOnUiThread {
                        listener.invoke(false, "")
                        dismiss()
                    }
                }
            }

        }

    }

}